package com.matt.repository.course;

import com.matt.domain.course.CoursePlan;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/5
 * *******************************
 */
public interface CoursePlanRepository extends JpaRepository<CoursePlan, String> {

    List<CoursePlan> findByIdIn(List<String> ids);

    List<CoursePlan> findByCourseDateGreaterThanEqualAndTeacherIdAndAreaIdOrderByCourseDateAscBeginTimeAscEndTimeAsc(Date currentDay, String teacherId, String areaId);


    @Query(nativeQuery = true, value = "SELECT c.* FROM course_plan c WHERE c.courseDate <?1 and c.teacherId=?2 and c.areaId=?3 ORDER BY c.courseDate desc limit ?4, ?5")
    List<CoursePlan> findByCourseDateAfterAndTeacherIdAndAreaId(Date currentDay, String teacherId, String areaId, Integer begin, Integer end);

    Long countByCourseDateAfterAndTeacherIdAndAreaId(Date currentDay, String teacherId, String areaId);

    List<CoursePlan> findByIdIn(String... planIds);

    List<CoursePlan> findByTeachingAreaIdAndCourseDate(String teachingAreaId, Date courseDate);

    List<CoursePlan> findByTeacherIdAndCourseDate(String teacherId, Date courseDate);

    List<CoursePlan> findByPrepareStatusAndCourseDateGreaterThanEqualAndCourseDateLessThanEqual(Integer prepareStatus, Date begin, Date end);

    @Query("update CoursePlan c set c.courseStatus = :status where c.courseDate <= :courseDate and  c.courseStatus = :courseStatus")
    @Transactional
    @Modifying
    Integer upadteCourseStatusByCourseDateGreaterThanEqual(@Param("courseDate") Date courseDate, @Param("status") Integer status, @Param("courseStatus") Integer courseStatus);

    List<CoursePlan> findByIdIn(List<String> ids, Pageable pageRequest);

    long countByIdIn(List<String> ids);

    List<CoursePlan> findByIdInAndBeginTimeAfterOrderByCourseDateAsc(List<String> ids,Date date);
}
